package com.bestv.search.common.service.impl;

import com.bestv.search.common.dao.SearchKeywordHistoryDao;
import com.bestv.search.common.dao.TopKeywordDao;
import com.bestv.search.common.dataSource.DataSourceKey;
import com.bestv.search.common.model.TopKeyword;
import com.bestv.search.common.service.TopKeywordManager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author Michael Wang
 */

// $Id: TopKeywordManagerImpl.java 163 2013-03-25 03:46:39Z luo.xiaojun $
public class TopKeywordManagerImpl implements TopKeywordManager {
    private TopKeywordDao topKeywordDao;
    private SearchKeywordHistoryDao searchKeywordHistoryDao;

    public List<TopKeyword> getTopKeywordByKeyword(DataSourceKey key, String keyword) {
        return topKeywordDao.getTopKeywordByKeyword(keyword);
    }

    public void saveTopKeywords(DataSourceKey key, List<TopKeyword> storeList) {
        topKeywordDao.saveTopKeywords(storeList);
    }

    public void removeTopKeyword(DataSourceKey key, String keyword) {
        topKeywordDao.removeTopKeyword(keyword);
    }

    public List<TopKeyword> getTotalTopKeywords(DataSourceKey key, int limit) {
        List<TopKeyword> keywords = new ArrayList<TopKeyword>();
        List<TopKeyword> dbTopKeywords = topKeywordDao.getAll();

        // Order by count desc
        Collections.sort(dbTopKeywords, new Comparator<TopKeyword>() {
            public int compare(TopKeyword t1, TopKeyword t2) {
                return t2.getCount() - t1.getCount();
            }
        });

        // Gets top N keywords
        int n = Math.min(limit, dbTopKeywords.size());
        for (int i = 0; i < n; i++) {
            keywords.add(dbTopKeywords.get(i));
        }
        return keywords;
    }

    public List<TopKeyword> getAllTopKeywords(DataSourceKey key) {
        return topKeywordDao.getAll();
    }

    public List<TopKeyword> getTopKeywords(DataSourceKey key, int start, int limit) {
        return topKeywordDao.getTopKeywords(start, limit);
    }

    public List<TopKeyword> getTopKeywordByKeyword(DataSourceKey key, String keyword, int start, int limit) {
        return topKeywordDao.getTopKeywordByKeyword(keyword, start, limit);
    }

    public int getTotalCount(DataSourceKey key) {
        return topKeywordDao.getTotalCount();
    }

    public int getTotalCountByKeyword(DataSourceKey key, String keyword) {
        return topKeywordDao.getTotalCountByKeyword(keyword);
    }

    public void setTopKeywordDao(TopKeywordDao topKeywordDao) {
        this.topKeywordDao = topKeywordDao;
    }

    public SearchKeywordHistoryDao getSearchKeywordHistoryDao() {
        return searchKeywordHistoryDao;
    }

    public void setSearchKeywordHistoryDao(SearchKeywordHistoryDao searchKeywordHistoryDao) {
        this.searchKeywordHistoryDao = searchKeywordHistoryDao;
    }

    public void saveTopKeyword(DataSourceKey key,TopKeyword topKeyword){
        topKeywordDao.saveTopKeyword(topKeyword);
    }
}
